package com.zdsoft.finance.busiform.service.impl;

import com.zdsoft.essential.client.service.CED;
import com.zdsoft.finance.busiform.entity.BusiForm;
import com.zdsoft.finance.busiform.repository.BusiFormRepository;
import com.zdsoft.finance.busiform.service.BusiFormService;
import com.zdsoft.finance.common.exception.BusinessException;
import com.zdsoft.framework.core.common.util.ObjectHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service("busiFormService")
public class BusiFormServiceImpl implements BusiFormService {

	@Autowired
	BusiFormRepository busiFormRepository;

    @Autowired
    private CED CED;



	@Override
	public BusiForm findByBusiFormNo(String busiFormNo) {
		return busiFormRepository.findByBusiFormNo(busiFormNo);
	}

	@Override
	public BusiForm findByBusinessIdAndBusinessType(String businessId, String businessType) {
		return busiFormRepository.findByBusinessIdAndBusinessType(businessId, businessType);
	}

	@Override
	public String applyBusiFormNo() {
		return null;
	}

	@Override
    @Transactional
	public BusiForm save(BusiForm busiForm) {
        return busiFormRepository.saveEntity(busiForm);
	}

	@Override
    @Transactional
	public BusiForm update(BusiForm busiForm) {
		BusiForm target = busiFormRepository.findOne(busiForm.getId());
		target = copyPoperties(busiForm, target);
		return busiFormRepository.updateEntity(target);
	}

	@Override
    @Transactional
	public BusiForm logicDelete(String businessId, String businessType) {
		BusiForm busiForm = busiFormRepository.findByBusinessIdAndBusinessType(businessId, businessType);
		busiForm=busiFormRepository.logicDelete(busiForm);
        return busiForm;
	}

    @Override
    public BusiForm findById(String id) throws BusinessException {
        if(ObjectHelper.isNotEmpty(id)){
            return this.busiFormRepository.findOne(id);
        }else {
            throw new BusinessException("1001001","通过ID查找busiform出错！参数为空");
        }
    }

    @Override
    @Transactional
    public BusiForm updateBusiFormStatus(BusiForm busiform, Integer status) throws BusinessException {
        BusiForm newBusiFrom=this.findById(busiform.getId());
        newBusiFrom.setStatus(status.intValue());
        return this.busiFormRepository.updateEntity(newBusiFrom);
    }

    @Override
    public List<BusiForm> findByCondition(Map<String, Object> condition) {
        return null;
    }

    /**
	 * 手动copy字段属性值
	 * 
	 * @param source
	 *            源数据
	 * @param target
	 *            目标数据
	 * @return copy后的对象
	 */
	private BusiForm copyPoperties(BusiForm source, BusiForm target) {
		if (ObjectHelper.isNotEmpty(source.getBusiFormNo())) {
			target.setBusiFormNo(source.getBusiFormNo());
		}
		if (ObjectHelper.isNotEmpty(source.getBusiTypeCd())) {
			target.setBusiTypeCd(source.getBusiTypeCd());
		}
		if (ObjectHelper.isNotEmpty(source.getBusiTypeNm())) {
			target.setBusiTypeNm(source.getBusiTypeNm());
		}
		if (ObjectHelper.isNotEmpty(source.getStatus())) {
			target.setStatus(source.getStatus());
		}
		if (ObjectHelper.isNotEmpty(source.getApplyEmpCd())) {
			target.setApplyEmpCd(source.getApplyEmpCd());
		}
		if (ObjectHelper.isNotEmpty(source.getApplyEmpNm())) {
			target.setApplyEmpNm(source.getApplyEmpNm());
		}
		if (ObjectHelper.isNotEmpty(source.getApplyTime())) {
			target.setApplyTime(source.getApplyTime());
		}
		if (ObjectHelper.isNotEmpty(source.getCompleteTime())) {
			target.setCompleteTime(source.getCompleteTime());
		}
		if (ObjectHelper.isNotEmpty(source.getTerminateTime())) {
			target.setTerminateTime(source.getTerminateTime());
		}
		if (ObjectHelper.isNotEmpty(source.getBusinessId())) {
			target.setBusinessId(source.getBusinessId());
		}
		if (ObjectHelper.isNotEmpty(source.getBusinessType())) {
			target.setBusinessType(source.getBusinessType());
		}
		if (ObjectHelper.isNotEmpty(source.getProcessInstanceKey())) {
			target.setProcessInstanceKey(source.getProcessInstanceKey());
		}
		if (ObjectHelper.isNotEmpty(source.getMeetingProjectId())) {
			target.setMeetingProjectId(source.getMeetingProjectId());
		}
        if(ObjectHelper.isNotEmpty(source.getCreateBy())){
            target.setCreateBy(source.getCreateBy());
        }
        if(ObjectHelper.isNotEmpty(source.getUpdateTime())){
            target.setCreateTime(source.getCreateTime());
        }
        if(ObjectHelper.isNotEmpty(source.getUpdateOrgCd())){
            target.setUpdateOrgCd(source.getUpdateOrgCd());
        }
        if(ObjectHelper.isNotEmpty(source.getUpdateBy())){
            target.setUpdateBy(source.getUpdateBy());
        }
        if(ObjectHelper.isNotEmpty(source.getCreateOrgCd())){
            target.setCreateOrgCd(source.getCreateOrgCd());
        }
        if(ObjectHelper.isNotEmpty(source.getCreateTime())){
            target.setCreateTime(source.getCreateTime());
        }
		return target;
	}

}
